import http from '../../services/http';

const state = {
	bannerOption: {
		autoplay: 3500,
		setWrapperSize: true,
		pagination: '.swiper-pagination',
		paginationClickable: true,
		mousewheelControl: true,
		autoplayDisableOnInteraction: false,
		observeParents: true,
		loop: true
	},
	friendOption: {
		setWrapperSize: true,
		pagination: '.swiper-pagination',
		paginationClickable: true,
		mousewheelControl: true,
		observeParents: true,
		loop: true
	},
	banners: [],
	business: [],
	friends: [],
	cases: [],
}

const mutations = {
	setBannerOption(state, payload) {
		state.bannerOption = { 
			...state.bannerOption,
			loop: payload.loop
		};
	},
	setFriendOption(state, payload) {
		state.friendOption = { 
			...state.friendOption,
			loop: payload.loop
		};
	},
	setBanner(state, payload) {
		state.banners = payload.banners;
	},
	setBusiness(state, payload) {
		state.business = payload.business;
	},
	setFriends(state, payload) {
		state.friends = payload.friends;
	},
	setCases(state, payload) {
		state.cases = payload.cases;
	},
}

const actions = {

	getBanner({ commit }, payload) {
		return new Promise((resolve, reject) => {
			http.get("/isp/api/v1/public/banner/list", (redata) => {
				if(redata.success) {
					let banners = [];
					if(typeof redata.data == 'object') {
						redata.data.forEach(item => {
							banners.push(item.imgUrl);
						})
						commit({
							type: 'setBanner',
							banners: banners
						});
						if(banners.length <= 1) {
							commit({
								type: 'setBannerOption',
								loop: false
							});
						}
						resolve(redata);
					};
				} else {
					reject('获取banner--失败')
				}
			})
		})
	},

	getBusiness({ commit }, payload) {
		return new Promise((resolve, reject) => {
			http.get("/isp/api/v1/public/service/serviceTree", (redata) => {
				if(redata.success) {
					let data = redata.data.splice(0, 6);
					console.log(data);
					data.push({
						icon: "/static/img/icon/home-all.png",
						link: "ServeAll",
						name: "全部"
					});
					commit({
						type: 'setBusiness',
						business: data
					});
					resolve(redata);
				} else {
					reject('获取服务菜单--失败');
				}
			})
		})
	},

	getPartner({ commit }, payload) {
		return new Promise((resolve, reject) => {
			http.get("/isp/api/v1/public/partner/list", {
				page: 0,
				size: 40
			}, (redata) => {
				let friendAll = [],
					cases = [],
					friends = [];
				if(redata.success) {
					redata.data.content.forEach(item => {
						if(item.partnerType == "PARTNER") {
							friendAll.push(item);
						} else {
							cases.push(item);
						}
					})
					do {
						friends.push(friendAll.splice(0, 9));
					} while (friendAll.length != 0);
					commit({
						type: 'setFriends',
						friends: friends
					});
					commit({
						type: 'setCases',
						cases: cases
					});
					if(friends.length <= 1) {
						commit({
							type: 'setFriendOption',
							loop: false
						});
					}
					resolve(friends, cases);
				} else {
					reject('获取合作伙伴--失败');
				}
			})
		})
	},

}

export default {
	state,
	mutations,
	actions
}